package com.example.library_common.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.library_common.constant.ApiTokenKt;
import com.example.library_common.utils.even.MessageWrap;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

public abstract class BaseFragmentEx extends BaseFragment {

    public abstract Object getLayoutId();

    protected View rootView = null;

    protected boolean mViewCreated = false;

    //fragment回退是否处理
    protected boolean mSuperBackPressedSupport = true;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (rootView != null) {

            ViewGroup parent = (ViewGroup) rootView.getParent();

            if (parent != null) {

                parent.removeView(rootView);
            }

        } else {

            Object layout = getLayoutId();

            if (layout instanceof Integer) {

                int layoutId = (int) getLayoutId();

                rootView = inflater.inflate(layoutId, container, false);

            } else if (layout instanceof View) {
                rootView = (View) getLayoutId();
            } else {

                throw new ClassCastException("type of setLayout() must be int or View!");

            }
        }

        mViewCreated = true;
        return rootView;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageWrap msg) {
        handMessage(msg);
//        if (message.message == ADD_DELIVERY_INFO) {
//            getAddressInfo()
//        }
    }

    protected void handMessage(MessageWrap msg) {

    }

//    @Override
//    public FragmentAnimator onCreateFragmentAnimator() {
//        return new DefaultHorizontalAnimator();
//    }

    @Override
    public void start(ISupportFragment toFragment) {
        Fragment parentFragment = getParentFragment();
        if (parentFragment instanceof BaseFragmentEx) {
            BaseFragmentEx fragment = (BaseFragmentEx) parentFragment;
            fragment.start(toFragment);
        } else {
            super.start(toFragment);
        }

    }

    @Override
    public boolean onBackPressedSupport() {
        FragmentManager childFragmentManager = getChildFragmentManager();
        FragmentManager manager = _mActivity.getSupportFragmentManager();
        int backStackEntryCount = manager.getBackStackEntryCount();
        int childBackStackEntryCount = childFragmentManager.getBackStackEntryCount();
        if (mSuperBackPressedSupport) {

            if (childBackStackEntryCount > 1) {
                popChild();
            } else if (backStackEntryCount > 1) {
                pop();
            } else {
                Fragment parentFragment = getParentFragment();
                if (parentFragment instanceof ISupportFragment) {
                    return ((ISupportFragment) parentFragment).onBackPressedSupport();
                }
                mSuperBackPressedSupport = false;
            }
//            return mSuperBackPressedSupport || super.onBackPressedSupport();
        } else {
            Fragment parentFragment = getParentFragment();
            if (parentFragment instanceof ISupportFragment) {
                return ((ISupportFragment) parentFragment).onBackPressedSupport();
            }
            pop();
            return true;
        }
        return mSuperBackPressedSupport;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ApiTokenKt.setCLICK_BLE(true);
        EventBus.getDefault().unregister(this);
    }
}
